<template>
	<view style="margin-top:400rpx;">
		<view style="width: 100%;text-align: center;">当前地址:{{area}}</view>
		<view
			style="
		margin-left:35%;margin-top:60rpx;
		width:250rpx;height:250rpx;line-height:250rpx;color:white;background-color: #60a5fa;border-radius: 150px;text-align: center;"
			@tap="getLocation(1)">
			打卡
		</view>
	</view>
</template>

<script>
	import cityPicker from '@/uni_modules/piaoyi-cityPicker/components/piaoyi-cityPicker/piaoyi-cityPicker';
	import config from "@/config/config.js";
	import xcUniappUtils from '@/library/xcore/js/XcUniappUtils.js';
	import xcViewUtils from '@/library/xcore/js/XcViewUtils.js';
	import xcHttpUtils from '@/library/xcore/js/XcHttpUtils.js';
	
	import xcCloginUtils from '@/library/xcore/js/XcCloginUtils.js';
	import {
		xsStringUtils,
		xsDateUtils,
		xsArrayUtils
	} from 'xstack-for-javascript';
	
	export default {
		data() {
			return {
				area: '用户未授权，获取当前位置失败'
			}
		},
		onShow() {
			this.getLocation(2)
		},
		methods: {
			getLocation(type) {
				uni.getLocation({
					type: 'wgs84', // 返回可以用于 wx.openLocation 的经纬度
					success: (res) => {
						const latitude = res.latitude;
						const longitude = res.longitude;
						const speed = res.speed;
						const accuracy = res.accuracy;
						var that = this
						uni.request({
							url: 'https://shanghaihuxi.cn/api/index/location',
							data: {
								longitude: longitude,
								latitude: latitude,
							},
							header: {
								'Content-Type': 'application/json' // 请求头，可以根据接口要求修改
							},
							success: (res2) => {
								console.log(222, res2.data.data.formatted_address)

								that.area = res2.data.data.formatted_address
								if(type == 1){
									uni.showModal({
										title: '提示',
										content: '当前地址:' + that.area,
										showCancel: true, // 是否显示取消按钮，默认为 true
										cancelText: '取消打卡',
										confirmText: '确定打卡',
										success: (res3) => {
											if (res3.confirm) {
									      	xcCloginUtils.getUserinfo(function(userinfo) {
												console.log(5555,that.area)
												console.log(66666,res2.data.data.addressComponent.city)
												uni.request({
													url: 'https://shanghaihuxi.cn/api/index/daka',
													data: {
														'user_id': userinfo.id,
														'area': that.area,
														'city': res2.data.data.addressComponent.city
													},
													header: {
														'Content-Type': 'application/json' // 请求头，可以根据接口要求修改
													},
													success: (res) => {
														uni.showToast({
															title: '打卡成功',
															icon: 'success',
															duration: 2000,
															success: () => {
																setTimeout
																	(() => {
									
																			uni.navigateTo({
																				url: '/pages/xclogin/me'
																			});
																		},
																		2000
																	);
															}
														});
													}
												});
									});
											} else if (res.cancel) {
									
											}
										}
									});
								}
					
							}
						});


					},
					fail: (err) => {
						console.error('获取位置失败：', err);
						uni.showToast({
							title: '获取位置失败，请检查权限或网络',
							icon: 'none'
						});
					}
				});
			}

		},
		watch: {

		}
	}
</script>

<style lang="scss" scoped>

</style>